<?php

class ContactPage extends Page
{

    static $db = array(
        'CompleteMessage' => 'HTMLText',
        'GoogleMap' => 'HTMLText',
        'MapLatitude' => 'Varchar(255)',
        'MapLongitude' => 'Varchar(255)'
    );

    static $has_one = array(

    );
    static $has_many = array(

    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->findOrMakeTab('Root.GoogleMapTab', 'Google Map');
        $fields->addFieldsToTab('Root.GoogleMapTab', array(
            new TextField('MapLatitude', 'Latitude'),
            new TextField('MapLongitude', 'Longitude')
        ));

        $fields->addFieldsToTab('Root.ThankYou', array(
            $completeMessage = new HtmlEditorField('CompleteMessage', 'Complete Message'),
        ));

        $GFCongig = GridFieldConfig::create()->addComponents(
            new GridFieldDataColumns(), new GridFieldToolbarHeader(), new GridFieldDeleteAction(), new GridFieldEditButton(), new GridFieldDetailForm(), new GridFieldSortableHeader(), new GridFieldPaginator(10)
        );
        $submitGF = new GridField(
            'Reservation', 'Submission', Reservation::get(), $GFCongig
        );
        $fields->findOrMakeTab('Root.Submission', 'Submission');
        $fields->addFieldsToTab('Root.Submission', array(
            $submitGF,
        ));

        return $fields;
    }
}

class ContactPage_Controller extends Page_Controller
{
    static $allowed_actions = array('contactForm');

    public function init()
    {
        parent::init();
    }

    function contactForm() {
        $fields = new FieldList(
            new TextField('Name', 'Name'),
            new TextField('Address', 'Address'),
            new TextField('Phone', 'Phone'),
            new TextField('Email', 'Mail'),
            new TextareaField('Message', 'Message'),
            new RecaptchaField('Captcha', 'Captcha')
        );
        $validator = new RequiredFields(array('Name', 'Email', 'Message'));
        $actions = new FieldList(
            new FormAction('doSaveContactForm', 'Send')
        );

        return new Form($this, 'contactForm', $fields, $actions, $validator);
    }

    function doSaveContactForm($data = null, $form = null) {

        $host = isset($_SERVER['HTTP_HOST'])? $_SERVER['HTTP_HOST'] : 'website';

        $noreply = 'no-reply@' . $host;

        $object_contact = new Reservation($data);
        $form->saveInto($object_contact);
        $object_contact->write();

        $siteConfig = SiteConfig::current_site_config();
        $adminMail = new RegisterSuccessEmail();
        $adminMail->setTemplate('ContactSubmissionEmail_admin');
        $adminMail->populateTemplate($data);
        $adminMail->setFrom($object_contact->Email);
        //$adminMail->setFrom($siteConfig->AdminEmail ? $siteConfig->AdminEmail : 'developer.epal@gmail.com');
        $adminMail->fromName = $object_contact->Name;//$siteConfig->FromName ? $siteConfig->FromName : $noreply;
        $adminMail->setSubject('New client has submitted Contact Form on Can Viet My website');
        //$adminMail->setTo($this->EmailTo);
        $adminMail->setTo($siteConfig->AdminEmail ? $siteConfig->AdminEmail : 'developer.epal@gmail.com');
        $adminMail->send();

        Controller::curr()->redirect($this->Link('?success'));
    }

    function isSuccess() {
        return isset($_GET['success']);
    }

}
